﻿// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");
using System;

// 账号类
public class Account
{
    // 字段
    public string accountNumber { get; set; }
    public string accountHolder { get; set; }
    public double balance { get; protected set; }

    public Account(string accountNumber, string accountHolder, double initialBalance)
    {
        this.accountNumber = accountNumber;
        this.accountHolder = accountHolder;
        this.balance = initialBalance;
    }

    // 方法
    public virtual void Deposit(double amount)
    {
        balance += amount;
        Console.WriteLine($"{accountNumber}账户成功存入 {amount} 元，当前余额: {balance} 元");
    }

    public virtual bool Withdraw(double amount)
    {
        if (amount > 0 && amount <= balance)
        {
            balance -= amount;
            Console.WriteLine($"{accountNumber}账户成功取出 {amount} 元，当前余额: {balance} 元");
            return true;
        }
        else
        {
            Console.WriteLine("余额不足。");
            return false;
        }
    }

    // 索引器
    public string this[int index]
    {
        get
        {
            if (index == 0)
            {
                return accountNumber;
            }
            else if (index == 1)
            {
                return accountHolder;
            }
            else
            {
                return null;
            }
        }
    }
}

// 信用账号类，继承自 Account 类
public class CreditAccount : Account
{
    public double creditLimit { get; set; }

    public CreditAccount(string accountNumber, string accountHolder, double initialBalance, double creditLimit)
        : base(accountNumber, accountHolder, initialBalance)
    {
        this.creditLimit = creditLimit;
    }

    // 覆盖 Withdraw 方法
    public override bool Withdraw(double amount)
    {
        if (amount > 0 && amount <= balance + creditLimit)
        {
            if (amount > balance)
            {
                double overdraft = amount - balance;
                creditLimit -= overdraft;
                balance = 0;
            }
            else
            {
                balance -= amount;
            }
            Console.WriteLine($"{accountNumber}账户成功取出 {amount} 元，当前余额: {balance} 元，剩余信用额度: {creditLimit} 元");
            return true;
        }
        else
        {
            Console.WriteLine("取款金额无效或超出信用额度。");
            return false;
        }
    }
}

// 银行类
public class Bank
{
    // 字段
    public string bankName { get; set; }
    private Account[] accounts;
    private int bankNum;

    public Bank(string bankName, int accountCapacity, int bankNum)
    {
        this.bankName = bankName;
        accounts = new Account[accountCapacity];
        this.bankNum = bankNum;
    }

    // 方法
    public void AddAccount(Account account)
    {
        if (bankNum < accounts.Length)
        {
            accounts[bankNum] = account;
            Console.WriteLine($"账号 {account.accountNumber} 已成功添加到 {bankName}。");
            bankNum++;
            return;
        }
        else
            Console.WriteLine("银行账户容量已满，无法添加新账户。");
    }

    public Account GetAccount(string accountNumber)
    {
        for (int i = 0; i < bankNum; i++)
        {
            if (accounts[i].accountNumber == accountNumber)
            {
                return accounts[i];
            }
        }
        Console.WriteLine($"未找到账号 {accountNumber}。");
        return null;
    }
}

// ATM 类
public class ATM
{
    // 字段
    private Bank bank;

    // 构造函数
    public ATM(Bank bank)
    {
        this.bank = bank;
    }

    // 方法
    public void Deposit(string accountNumber, double amount)
    {
        Account account = bank.GetAccount(accountNumber);
        if (account != null)
        {
            account.Deposit(amount);
        }
    }

    public void Withdraw(string accountNumber, double amount)
    {
        Account account = bank.GetAccount(accountNumber);
        if (account != null)
        {
            account.Withdraw(amount);
        }
    }
}

class Program
{
    static void Main()
    {
        Bank bank = new Bank("xx 银行", 10, 0);

        Account regularAccount = new Account("111", "张三", 1000);
        bank.AddAccount(regularAccount);

        CreditAccount creditAccount = new CreditAccount("112", "李四", 500, 2000);
        bank.AddAccount(creditAccount);

        ATM atm = new ATM(bank);


        atm.Deposit("111", 500);
        atm.Withdraw("111", 200);

        atm.Deposit("112", 500);
        atm.Withdraw("112", 1300);
    }
}